package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

// 购物车
@Service
@Transactional
public class CartServiceImpl implements CartService {


    @Autowired
    private CartMapper cartMapper;

    // 添加购物车
    @Override
    public Cart add(Cart cartParam) {
        // 1. 查询是否有此购物项记录 select * from shopping_cart where user_id= ?  and (dish_id=?  | setmeal_id =?)
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        // 1-1 用户id
        Long userId = UserHolder.get().getId();
        qw.eq(Cart::getUserId,userId);
        // 1-2 菜品id
        qw.eq(cartParam.getDishId()!=null,Cart::getDishId,cartParam.getDishId());
        // 1-3 套餐id
        qw.eq(cartParam.getSetmealId()!=null,Cart::getSetmealId,cartParam.getSetmealId());
        // 1-4 执行查询
        Cart cart = cartMapper.selectOne(qw);
        // 判断
        if (cart!=null) {
            // 2.数量+1 ，更新数据库
            cart.setNumber(cart.getNumber()+1);
            cartMapper.updateById(cart);
        }else{
            // 3.补全信息，添加数据库
            cart=cartParam;
            cart.setUserId(userId); // 用户id
            cart.setNumber(1); // 购买数量
            cart.setCreateTime(new Date()); // 添加时间
            cartMapper.insert(cart);
        }

        // 4.返回购物车
        return cart;
    }

    // 查看购物车列表
    @Override
    public List<Cart> list() {
        // 1.构建条件  select * from shopping_cart where user_id = ?
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        qw.eq(Cart::getUserId,UserHolder.get().getId());

        // 2.执行查询
        return cartMapper.selectList(qw);
    }

    // 清空购物车
    @Override
    public void clean() {
        // 1.构建条件 delete from shopping_cart where user_id = ?
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        qw.eq(Cart::getUserId,UserHolder.get().getId());

        // 2.执行删除
        cartMapper.delete(qw);
    }

    // 修改购物车
    @Override
    public Cart sub(Cart cartParam) {
        // 1.先查询购物车
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        // 用户id
        qw.eq(Cart::getUserId, UserHolder.get().getId());
        // 套餐id 动态sql
        qw.eq(cartParam.getSetmealId() != null, Cart::getSetmealId, cartParam.getSetmealId());
        // 菜品id 动态sql
        qw.eq(cartParam.getDishId() != null, Cart::getDishId, cartParam.getDishId());
        Cart cart = cartMapper.selectOne(qw);

        // 2.购物数量-1
        cart.setNumber(cart.getNumber()-1);

        // 3.判断数量是否为0
        if (cart.getNumber()==0) {
            // 删除购物车
            cartMapper.deleteById(cart.getId());
        }else{
            // 更新购物车
            cartMapper.updateById(cart);
        }
        // 4.返回购物车
        return cart;
    }
}
